<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-form :inline="true">
        <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="startAdd">添加</el-button>
        <el-button class="filter-item" style="float:right" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
        <el-form-item label="角色名称" style="float:right">
          <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 120px;" placeholder=""/>
        </el-form-item>

      </el-form>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column align="center" label="角色名称" prop="name" />

      <el-table-column align="center" label="角色描述" prop="description" />

      <el-table-column align="center" label="权限管理">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handlePermission(scope.row)">【权限管理】</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="startEditRole(scope.row)">编辑</el-button>
          <el-button type="primary" size="small" @click="deleteRole(scope.row.id, scope.row.name)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!--<pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList" />-->

    <el-dialog :close-on-click-modal="false" :title="dialogTitle" :visible.sync="dialogFormVisible" width="40%">
      <el-form :inline="true" status-icon>
        <el-row type="flex" justify="center">
          <el-col :span="22">
            <el-form-item label="* 角色名称 " label-width="10em">
              <el-input v-model="accountModel.name" clearable class="filter-item" style="width: 260px;" placeholder="请输入角色名称"/>
            </el-form-item>
            <el-form-item label="* 角色描述 " label-width="10em">
              <el-input v-model="accountModel.description" type="textarea" clearable class="filter-item" style="width: 260px;" placeholder="请输入角色描述"/>
            </el-form-item>

          </el-col>
        </el-row>

      </el-form>

      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="confirm(); dialogFormVisible = false">确定</el-button>
        <el-button @click="dialogFormVisible = false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :visible.sync="permissionDialogFormVisible" title="权限配置">
      <el-tree
        ref="tree"
        :data="systemPermissions"
        :default-checked-keys="assignedPermissions"
        show-checkbox
        node-key="id"
        highlight-current>
        <!--        <span slot-scope="{ node, data }" class="custom-tree-node">
          <span>{{ data.label }}</span>
          <el-tag v-if="data.api" type="success" size="small">{{ data.api }}</el-tag>
        </span>-->
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="permissionDialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="updatePermission">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { getRoleList, createRole, updateRole, deleteRole, getPermission, updatePermission } from '@/api/admin'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Role',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20
      },
      downloadLoading: false,
      dialogFormVisible: false,
      permissionDialogFormVisible: false,
      systemPermissions: null,
      assignedPermissions: null,
      dialogType: 1,
      dialogTitle: '',
      accountModel: {},
      permissionForm: {
        roleId: undefined,
        permissions: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getRoleList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.data.length
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    startEditRole(accountModel) {
      if (accountModel.id == 1) {
        this.$notify.success({
          title: '错误',
          message: '超级管理员角色不能编辑'
        })
        return
      }
      this.dialogFormVisible = true
      this.dialogType = 2
      this.dialogTitle = '编辑'
      this.accountModel = accountModel
    },
    confirm() {
      if (this.dialogType == 1) {
        createRole(this.accountModel).then(response => {
          this.getList()
        })
      } else if (this.dialogType == 2) {
        updateRole(this.accountModel).then(response => {
          this.getList()
        })
      }
    },
    startAdd() {
      this.dialogFormVisible = true
      this.dialogType = 1
      this.dialogTitle = '添加'
      this.accountModel = {}
    },
    deleteRole(adminId, username) {
      if (adminId == 1) {
        this.$notify.success({
          title: '错误',
          message: '超级管理员角色不能删除'
        })
        return
      }
      this.$confirm('确定删除: ' + username + ' ？')
        .then(_ => {
          const resetData = { 'id': adminId }
          deleteRole(resetData).then(response => {
            this.getList()
          })
        })
        .catch(_ => {})
    },
    handlePermission(row) {
      if (row.id == 1) {
        this.$notify.success({
          title: '错误',
          message: '超级管理员角色不能编辑'
        })
        return
      }
      this.permissionDialogFormVisible = true
      this.permissionForm.roleId = row.id
      getPermission({ id: row.id })
        .then(response => {
          this.systemPermissions = response.data.data.systemPermissions
          this.assignedPermissions = response.data.data.assignedPermissions
        })
    },
    updatePermission() {
      this.permissionForm.permissions = this.$refs.tree.getCheckedKeys(true)
      console.log(this.permissionForm.permissions)
      updatePermission(this.permissionForm)
        .then(response => {
          this.permissionDialogFormVisible = false
          this.$notify.success({
            title: '成功',
            message: '授权成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    }
  }
}
</script>
